package repository

import (
	"context"
	"rdiot-demo/internal/eav/domain"

	"github.com/google/uuid"
)

// Repositories 仓储集合
type Repositories struct {
	TemplateRepository TemplateRepository
	EntityRepository   EntityRepository
}

// TemplateRepository 模板仓储接口
type TemplateRepository interface {
	Create(ctx context.Context, template *domain.EntityTemplate) error
	GetByID(ctx context.Context, id uuid.UUID) (*domain.EntityTemplate, error)
	GetByName(ctx context.Context, name string) (*domain.EntityTemplate, error)
	Update(ctx context.Context, template *domain.EntityTemplate) error
	Delete(ctx context.Context, id uuid.UUID) error
	List(ctx context.Context, filter TemplateFilter) ([]domain.EntityTemplate, int64, error)
}

// EntityRepository 实体仓储接口
type EntityRepository interface {
	Create(ctx context.Context, entity *domain.Entity, attributes map[string]interface{}) (*domain.Entity, error)
	GetByID(ctx context.Context, id uuid.UUID) (*domain.Entity, error)
	GetByTemplateID(ctx context.Context, templateID uuid.UUID, page, size int) ([]*domain.Entity, int64, error)
	Update(ctx context.Context, entity *domain.Entity, attributes map[string]interface{}) (*domain.Entity, error)
	Delete(ctx context.Context, id uuid.UUID) error
	Query(ctx context.Context, query EntityQuery) ([]*domain.Entity, int64, error)
	BatchCreate(ctx context.Context, entities []*domain.Entity, attributesMap map[uuid.UUID]map[string]interface{}) ([]*domain.Entity, error)

	// 属性值操作
	GetAttributes(ctx context.Context, entityID uuid.UUID) (map[string]interface{}, error)
}

// TemplateFilter 模板过滤器
type TemplateFilter struct {
	Name     string
	IsActive *bool
	Page     int
	Size     int
}

// EntityQuery 实体查询条件
type EntityQuery struct {
	TemplateID *uuid.UUID
	Name       *string
	Conditions []AttributeCondition
	OrderBy    []OrderByClause
	Page       int
	Size       int
}

// AttributeCondition 属性查询条件
type AttributeCondition struct {
	AttributeName string
	Operator      string
	Value         interface{}
	DataType      string
}

// OrderByClause 排序子句
type OrderByClause struct {
	Field     string
	Direction string
}
